//
//  ZLWRadioController.m
//  EPA
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWRadioController.h"
#import "ZLWRadioListModel.h"
#import "ZLWRadioLIstCell.h"
#import "ZLWRadioPlayerController.h"
#import "LORefresh.h"
#import "ZLW_NetWorkRequestManager.h"
@interface ZLWRadioController ()
/**
 *  接收的数组
 */
@property (nonatomic, strong) NSMutableArray *radioArray;

/**
 *  header属性
 */
@property (nonatomic, strong)LORefresh *headerRefresh;
@end

@implementation ZLWRadioController

-(void)dealloc
{
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.radioArray = [NSMutableArray new];
    //请求数据
    [self reloadNetWorking];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZLWRadioLIstCell" bundle:nil] forCellReuseIdentifier:@"RadioListCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Uncomment the following line to preserve selection between presentations.取消注释以下行保存报告之间的选择。
    //self.clearsSelectionOnViewWillAppear = NO;默认值为YES。如果是的,任何选择都是清除进行处理:
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
#pragma mark 上拉在家,下拉刷新
    __weak typeof(self)weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeHeaderGif) refreshingBlock:^{
        //移除
        [weakSelf.radioArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf reloadNetWorking];
    }];
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"wangnima"];
    
    [self.tableView addRefreshWithRefreshViewType:(LORefreshViewTypeFooterDefault) refreshingBlock:^{
        [weakSelf reloadNetWorking];
    }];

}
-(void)reloadNetWorking
{
    [ZLW_NetWorkRequestManager requestURL:[NSURL URLWithString:RADIODETAILLIST_URL] Method:CK_POST Params:@{@"auth":@"",@"client":@1,@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":self.radioAllarysModel.radioid,@"version":@"3.0.4"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            //            NSLog(@"%@",jsonDict);
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                ZLWRadioListModel *rModel = [ZLWRadioListModel new];
                [rModel setValuesForKeysWithDictionary:itemDict];
                [self.radioArray addObject:rModel];
                
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [[self.tableView gifHeader] endRefreshing];
            [[self.tableView defaultFooter] endRefreshing];
        });
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.radioArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLWRadioLIstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioListCell" forIndexPath:indexPath];
    
   cell.model = self.radioArray[indexPath.row];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLWRadioPlayerController *playerVC = [[ZLWRadioPlayerController alloc] init];
    playerVC.radioArray = self.radioArray;
    playerVC.currentIndex = indexPath.row;
    [self.navigationController pushViewController:playerVC animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
